package com.ecoolex.ying.space.admin.graphql;

import static com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type.Admin;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.admin.core.AdminConstant;
import com.ecoolex.ying.space.entity.EstateProjectActivity;
import com.ecoolex.ying.space.entity.EstateProjectActivityExternalGoods;
import com.ecoolex.ying.space.entity.EstateProjectActivityExternalGoodsCategory;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsCategory;
import com.ecoolex.ying.space.pojo.AddEstateProjectActivityExternalGoodsCategoryDTO;
import com.ecoolex.ying.space.pojo.EditEstateProjectActivityExternalGoodsCategoryDTO;
import com.ecoolex.ying.space.ws.EstateProjectActivityExternalGoodsService;
import com.ecoolex.ying.space.ws.EstateProjectActivityService;

@GraphQL2Module(type = Admin, description = "地产项目外部热门商品分类管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY, key = EstateProjectActivityExternalGoodsCategoryGraphql.AUTH, name = "地产项目外部热门商品分类管理")
public class EstateProjectActivityExternalGoodsCategoryGraphql {

	public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-ESTATE-PROJECT-ACTIVITY-EXTERNAL-GOODS-CATEGORY";
	
	private static final String AUTH_ADD = AUTH + "-ADD";
	
	private static final String AUTH_EDIT = AUTH + "-EDIT";
	
	private static final String AUTH_DEL = AUTH + "-DEL";
	
	private static final String AUTH_ONLINE = AUTH + "-ONLINE";
	
	private static final String AUTH_OFFLINE = AUTH + "-OFFLINE";
	
	private static final String AUTH_GET = AUTH + "-GET";
	
	private static final String AUTH_QUERY = AUTH + "-QUERY";
	
	@Reference
	private EstateProjectActivityExternalGoodsService service;
	
	@Reference
	private EstateProjectActivityService estateProjectActivityService;
	
	@TypeField(type = EstateProjectActivityExternalGoodsCategory.class,field = "canDelete",description = "能删除")
	public Boolean canDel(@Source EstateProjectActivityExternalGoodsCategory source,AdminMemberAuthority user) {
		return user.auth(AUTH_DEL);
	}
	
	@TypeField(type = EstateProjectActivityExternalGoodsCategory.class,field = "canOnline",description = "能上线")
	public Boolean canOnline(@Source EstateProjectActivityExternalGoodsCategory source) {
		return Check.eq(false, source.getOnline());
	}
	
	@TypeField(type = EstateProjectActivityExternalGoodsCategory.class,field = "canOffline",description = "能下线")
	public Boolean canOffline(@Source EstateProjectActivityExternalGoodsCategory source) {
		return Check.eq(true, source.getOnline());
	}
	
	@TypeField(type = EstateProjectActivityExternalGoodsCategory.class,field = "parentCategory",description = "父分类")
	public EstateProjectActivityExternalGoodsCategory parentCategory(@Source EstateProjectActivityExternalGoodsCategory source) {
		if (Check.notNullAndGtZero(source.getParentCategoryId())) {
			return service.getCategory(source.getParentCategoryId());
		}
		return null;
	}
	
	@TypeField(type = EstateProjectActivityExternalGoodsCategory.class,field = "childrenCategory",description = "子分类")
	public List<EstateProjectActivityExternalGoodsCategory> childrenCategory(@Source EstateProjectActivityExternalGoodsCategory source) {
		return service.childrenCategory(source.getExternalGoodsCategoryId());
	}
	
	@TypeField(type = EstateProjectActivityExternalGoodsCategory.class,field = "estateProjectActivity",description = "所属项目")
	public EstateProjectActivity estateProjectActivity(@Source EstateProjectActivityExternalGoodsCategory source) {
		return estateProjectActivityService.get(source.getProjectId());
	}
	
	@QueryField(field = "estateProjectActivityExternalGoodsCategory" , description = "地产项目外部热门商品分类")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "地产项目外部热门商品分类")
	public EstateProjectActivityExternalGoodsCategory get(@NonNull Integer externalGoodsCategoryId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		return service.getCategory(externalGoodsCategoryId);
	}
	
	@QueryField(field = "estateProjectActivityExternalGoodsCategoryList" , description = "地产项目外部热门商品分类列表")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "地产项目外部热门商品分类列表")
	public Pagination<EstateProjectActivityExternalGoodsCategory> query(@NonNull Integer projectId,String name,@NonNull Sorting[] sorts,@NonNull Page page,AdminMemberAuthority user){
		user.assertAuth(AUTH_QUERY);
		return service.queryCategory(projectId, name, sorts, page);
	}
	
	@MutationField(field = "addEstateProjectActivityExternalGoodsCategory", description = "添加地产项目外部热门商品分类")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加地产项目外部热门商品分类")
	public EstateProjectActivityExternalGoodsCategory add(@NonNull AddEstateProjectActivityExternalGoodsCategoryDTO record,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		EstateProjectActivity estateProjectActivity = estateProjectActivityService.get(record.getProjectId());
		Assert.isTrue(Check.notNull(estateProjectActivity),ResultCode.VERIFY.build(1), "地产项目不存在");
		return service.addCategory(record, user.userName());
	}
	
	@MutationField(field = "editEstateProjectActivityExternalGoodsCategory", description = "编辑地产项目外部热门商品分类")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑地产项目外部热门商品分类")
	public EstateProjectActivityExternalGoodsCategory edit(@NonNull EditEstateProjectActivityExternalGoodsCategoryDTO record,AdminMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		EstateProjectActivity estateProjectActivity = estateProjectActivityService.get(record.getProjectId());
		Assert.isTrue(Check.notNull(estateProjectActivity),ResultCode.VERIFY.build(1), "地产项目不存在");
		EstateProjectActivityExternalGoodsCategory old = service.getCategory(record.getExternalGoodsCategoryId());
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(2), "分类不存在");
		BeanUtil.copy(record, old);
		return service.editCategory(old, user.userName());
	}
	
	@MutationField(field = "delEstateProjectActivityExternalGoodsCategory", description = "删除地产项目外部热门商品分类")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除地产项目外部热门商品分类")
	public Boolean delete(@NonNull Integer externalGoodsCategoryId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_DEL);
		EstateProjectActivityExternalGoodsCategory old = service.getCategory(externalGoodsCategoryId);
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "分类不存在");
		service.deleteCategory(old, user.userName());
		return true;
	}
	
	@MutationField(field = "onlineEstateProjectActivityExternalGoodsCategory", description = "上线地产项目外部热门商品分类")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ONLINE, name = "上线地产项目外部热门商品分类")
	public Boolean online(@NonNull Integer externalGoodsCategoryId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ONLINE);
		EstateProjectActivityExternalGoodsCategory old = service.getCategory(externalGoodsCategoryId);
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "分类不存在");
		service.onlineCategory(old, user.userName());
		return true;
	}
	
	@MutationField(field = "offlineEstateProjectActivityExternalGoodsCategory", description = "下线地产项目外部热门商品分类")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_OFFLINE, name = "下线地产项目外部热门商品分类")
	public Boolean offline(@NonNull Integer externalGoodsCategoryId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_OFFLINE);
		EstateProjectActivityExternalGoodsCategory old = service.getCategory(externalGoodsCategoryId);
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "分类不存在");
		service.offlineCategory(old, user.userName());
		return true;
	}
}
